package hutech.ksv.view;

import hutech.ksv.entity.QuanTriVienEntity;
import hutech.ksv.entity.TaiKhoanQTVEntity;
import hutech.ksv.service.AdminAccountService;
import hutech.ksv.service.EncryptMD5;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class ThemAdminManagedBean implements Serializable {
	
	// Cac properties
		private static final long serialVersionUID = 1L;

		// Thong tin quan tri vien
		private String tenQTV;
		private String diaChiQTV;
		private String gioiTinhQTV;
		private String sdtQTV;
		private String emailQTV;

		// Tai khoan quan tri vien
		private String username;
		private String password;

		// Kiem tra dinh dang cua du lieu nhap vao
		private final String email_pattern = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
		private final String sdt_pattern = "^\\s*\\d+\\s*$";
		private static Pattern pattern;
		private static Matcher matcher;

		// Bao loi neu nhap sai dinh dang
		private String tinhTrangEmail;
		private String tinhTrangSDT;

		// Tinh trang tai khoan (da ton tai hay chua)
		private String tinhTrangTaiKhoan;

	@Autowired
	AdminAccountService adminAccountService;
	
	@Autowired
	EncryptMD5 encryptMD5;

	public String themQTV() {

		boolean isTaiKhoanTonTai = false;

		QuanTriVienEntity quanTriVienEntity = new QuanTriVienEntity();
		quanTriVienEntity.setTenQTV(tenQTV);
		quanTriVienEntity.setDiaChiQTV(diaChiQTV);
		quanTriVienEntity.setGioiTinhQTV(gioiTinhQTV);
		quanTriVienEntity.setSdtQTV(sdtQTV);
		quanTriVienEntity.setEmailQTV(emailQTV);		
		int maQTV = adminAccountService.themQuanTriVien(quanTriVienEntity);

		if (maQTV > 0) {
			TaiKhoanQTVEntity taiKhoanQTVEntity = new TaiKhoanQTVEntity();
			taiKhoanQTVEntity.setTenTKQTV(username);
			taiKhoanQTVEntity.setMatKhauQTV(encryptMD5.md5(password));
			taiKhoanQTVEntity.setMaQTV(maQTV);
			
			isTaiKhoanTonTai = adminAccountService.isTaiKhoanAdminTonTai(username);

			if (isTaiKhoanTonTai) {
				adminAccountService.xoaQTVVuaNhap(maQTV);
				tinhTrangTaiKhoan = ("<span class='error-message'><b>Tài khoản đã tồn tại!</b></span>");
				return "themAdmin";
			} else {
				int result = adminAccountService.themTaiKhoanQTV(taiKhoanQTVEntity);

				if (result > 0) {
					return "themAdmin";
				} else {
					return "themTaiKhoanAdminLoi";
				}
			}
	
		}
		return "";
	}

	public boolean checkValidEmail(String email) {
		pattern = Pattern.compile(email_pattern);
		matcher = pattern.matcher(email);
		return matcher.matches();

	}

	public boolean checkValidPhone(String sdt) {
		pattern = Pattern.compile(sdt_pattern);
		matcher = pattern.matcher(sdt);
		return matcher.matches();
	}

	/**
	 * 
	 */

	public String getTenQTV() {
		return tenQTV;
	}

	public void setTenQTV(String tenQTV) {
		this.tenQTV = tenQTV;
	}

	public String getDiaChiQTV() {
		return diaChiQTV;
	}

	public void setDiaChiQTV(String diaChiQTV) {
		this.diaChiQTV = diaChiQTV;
	}

	public String getGioiTinhQTV() {
		return gioiTinhQTV;
	}

	public void setGioiTinhQTV(String gioiTinhQTV) {
		this.gioiTinhQTV = gioiTinhQTV;
	}

	public String getSdtQTV() {
		return sdtQTV;
	}

	public void setSdtQTV(String sdtQTV) {
		if (checkValidPhone(sdtQTV)) {
			tinhTrangSDT = "";
		} else {
			tinhTrangSDT = ("<span class='error-message'><b>Số điện thoại sai định dạng!</b></span>");
		}
		this.sdtQTV = sdtQTV;
	}

	public String getEmailQTV() {
		return emailQTV;
	}

	public void setEmailQTV(String emailQTV) {
		if (checkValidEmail(emailQTV)) {
			tinhTrangEmail = "";
		} else {
			tinhTrangEmail = "<span class='error-message'><b>Email sai định dạng!</b></span>";
		}
		this.emailQTV = emailQTV;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTinhTrangEmail() {
		return tinhTrangEmail;
	}

	public void setTinhTrangEmail(String tinhTrangEmail) {
		this.tinhTrangEmail = tinhTrangEmail;
	}

	public String getTinhTrangSDT() {
		return tinhTrangSDT;
	}

	public void setTinhTrangSDT(String tinhTrangSDT) {
		this.tinhTrangSDT = tinhTrangSDT;
	}

	public String getTinhTrangTaiKhoan() {
		return tinhTrangTaiKhoan;
	}

	public void setTinhTrangTaiKhoan(String tinhTrangTaiKhoan) {
		this.tinhTrangTaiKhoan = tinhTrangTaiKhoan;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getEmail_pattern() {
		return email_pattern;
	}

	public String getSdt_pattern() {
		return sdt_pattern;
	}

}
